<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
     <title>编辑文档模板</title>
     <script type="text/javascript" src="${pageContext.request.contextPath}/cts/js/jquery.js"></script>
     <script type="text/javascript" src="${pageContext.request.contextPath}/cts/js/ckeditor/ckeditor.js"></script>
     <script type="text/javascript">
         $(function(){
             var urlError = '<s:property value="#request.urlError"/>';
             if(urlError == 1){
                alert("路径错误");
                window.location.href = "${pageContext.request.contextPath}/cts/findDocTempList?type=file";
             }else{
	             $.post('cts/findDocTempContent?id=<s:property value="docTemplate.id"/>' , "" , function(data){
	                 if(data.length > 0){
	                    CKEDITOR.instances.editor1.setData(data);
	                 }         
	             });
	           
	             CKEDITOR.replace('editor1', function(){});
	             var isEnable = '<s:property value="docTemplate.status"/>';
	             if(isEnable == '' || isEnable == 0){
	                $("#docStatus").attr("checked" , "checked");
	             }
	             
	             initshowCode();
             }
        });
        
        function initshowCode(){
           var code = '<s:property value="#request.code"/>';
           if(code!=''){
              if(code == 1){
                 //alert("保存成功");
                 //window.parent.location.reload();
                 //window.close();
                 if(confirm("保存成功，是否返回列表")){
                   window.location.href = "${pageContext.request.contextPath}/cts/findDocTempList?type=file";
                 }
              }else if(code == 0){
                 alert("保存失败");
              }
           }
        }
        
        function addUploadButton(editor){
           CKEDITOR.on('dialogDefinition', function( ev ){});
        }
        
        function saveDocTempc(){
           var content =  CKEDITOR.instances.editor1.getData();
           var name = $("#name").val();
           var checked = $("#docStatus").is(":checked") ? 0 : 1;
           var id = '<s:property value="docTemplate.id"/>';
           var oldName = '<s:property value="docTemplate.name"/>';
           
           if(name.replace(/\s/g , '') == ''){
             alert("文档名称不能为空");
             return;
           }
           
          $("#hstatus").val(checked);
          if(id!='' &&  name == oldName){
               document.getElementById("content").value = content;
	           var form = document.getElementById("docTempForm");
	           form.action = "cts/saveDocTemp.action";
	           form.submit();
          }else{
	           $.post("cts/vailDocTempReName?name="+name , "" , function(data){
	               if(data == 1){
			           document.getElementById("content").value = content;
			           var form = document.getElementById("docTempForm");
			           form.action = "cts/saveDocTemp.action";
			           form.submit();
	               }else if(data == -2){
	                  alert("操作失败");
	               }else{
	                   alert("文档模板名已存在!");
	               }
	           });
          }
           
        } 
     </script>
  
  </head>
  
  <body>
    <form action="saveDocTemp" id="docTempForm" name="n_docTempForm" method="post" theme="simple" enctype="multipart/form-data">
      <s:if test="docTemplate!=null">
         <input type="hidden" name="docTemplate.id" id="id" value="<s:property value="docTemplate.id"/>"/>
      </s:if>
      <input type="hidden" id="hstatus" name="docTemplate.status" value='<s:property value="docTemplate.status"/>'/>
      <input type="hidden" id="content" name="docTemplate.content" />
      <input type="hidden" id="companyid" name="docCompanyid" value='<s:property value="docTemplate.company.id"/>'/>
      
      <table style="font-size:12px;width:650px;border:1px solid gray;" align="center">
         <tr>
            <td>
              <div style="float:left;">
                    文档名称：<input type="text" name="docTemplate.name" id="name" value="<s:property value="docTemplate.name"/>"/>
              </div> 
              <div style="float:right;margin-right:20px;">  
                    是否启用：<input type="checkbox" value="status" id="docStatus" name="docStatus" value="0"/>
              </div>
              
            </td>
         </tr>
         <tr>
            <td>
		       <div align="center" style="margin: 0 auto;">
			       <textarea name="editor1" id="editor1"></textarea>
			   </div>
            </td>
         </tr>
         <tr>
            <td align="center">
               <input type="button" id="saveDocTemp" name="n_saveDocTemp" value="保存" onclick="saveDocTempc()"/>
            </td>
         </tr>
      </table>
    </form>
  </body>
</html>
